package com.step.ems.component

import androidx.annotation.StringRes
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.step.ems.R
import com.step.ems.common.UiState
import com.step.ems.ui.theme.ContentBg
import com.step.ems.ui.theme.DarkBlue
import com.valentinilk.shimmer.ShimmerBounds
import com.valentinilk.shimmer.defaultShimmerTheme
import com.valentinilk.shimmer.rememberShimmer
import com.valentinilk.shimmer.shimmer
import com.valentinilk.shimmer.shimmerSpec

@Composable
fun PageStatus(
    uiState: UiState,
    total: Int,
    retry: () -> Unit,
    padding: PaddingValues = PaddingValues(),
    content: @Composable () -> Unit
) {
    if (total > 0) {
        content()
    } else {
        when (uiState) {
            is UiState.Loading -> {
                PageShimmer(padding)
            }

            is UiState.Error -> {
                PageStatusRetry(R.string.errorTip, retry = retry)
            }

            else -> {
                PageStatusRetry(R.string.emptyTip, retry = retry)
            }
        }
    }
}

@Composable
fun PageStatusRetry(@StringRes id: Int, retry: () -> Unit) {
    Column(
        modifier = Modifier
            .fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Text(text = stringResource(id), fontSize = 16.sp)
        Spacer(modifier = Modifier.height(4.dp))
        Button(
            modifier = Modifier.height(33.dp),
            shape = RoundedCornerShape(2.dp),
            contentPadding = PaddingValues(horizontal = 18.dp, vertical = 2.dp),
            colors = ButtonDefaults.buttonColors(containerColor = DarkBlue),
            onClick = { retry() }) {
            Text(text = stringResource(id = R.string.retry), fontSize = 15.sp)
        }
    }
}

@Composable
fun PageShimmer(padding: PaddingValues = PaddingValues()) {
    val shimmer = rememberShimmer(
        shimmerBounds = ShimmerBounds.View,
        theme = defaultShimmerTheme.copy(
            animationSpec = infiniteRepeatable(
                animation = shimmerSpec(
                    durationMillis = 700,
                    easing = LinearEasing,
                    delayMillis = 300,
                ),
                repeatMode = RepeatMode.Restart,
            ),
        ),
    )
    Column(
        Modifier
            .fillMaxSize()
            .padding(padding)
            .background(ContentBg)
            .shimmer(shimmer)
    ) {
        for (i in 1..6) {
            Box(
                modifier = Modifier
                    .weight(1f)
                    .fillMaxWidth()
                    .background(Color.White)
            )
            Spacer(modifier = Modifier.height(4.dp))
        }
    }
}